<?php
#基础函数
include_once('_path.php');

if (BENCHMARK) {
	require_once(_CONST_LOCAL_INCLUDE_PATH.'/Benchmark/Timer.php');
	$timer = new Benchmark_Timer();
	$timer->start();
}

#db
include_once(_CONST_LOCAL_INCLUDE_PATH."database.class.php");
$database = new Database($db_vars);

if ($db_slave_vars['hostname'] != '') {
	$slave = new Database($db_slave_vars);
} else {
	$slave =& $database;
}

#通用函数
include_once(_CONST_LOCAL_INCLUDE_PATH."common.php");

if (BENCHMARK) $timer->setMarker('init');

#登录权限
check_loggedin();

#用户信息
include_once(_CONST_LOCAL_INCLUDE_PATH."session.include.php");

if (BENCHMARK) $timer->setMarker('auth');

#处理上一步骤
	
if(!isset($_SESSION['step_2'])){	
	$token = $_POST['token'];
	if(empty($token) || $token != $_SESSION['token']){
		//exit("Access Exit!");
		header("Location:"._CONST_WEB_URL."flow-1.php");
		exit;
	}
}
	
if (isset($_POST['step']) && $_POST['step'] == 'do') {
	#校验
	verify_field("gurl", "GUrl", "empty");
	verify_field("gurl", "GUrl", "url");
	verify_field("gimg", "GImg", "empty");
	verify_field("gimg", "GImg", "custom", "/^(http:\/\/).+(jpg|jpeg).*$/i");
	verify_field("title", "Title", "empty");
	if (count($error_strs)) {
		exit("Error Pargram Exit!");
	}

	$gurl = trim($_POST['gurl']);
	$gimg = trim($_POST['gimg']);
	$title = trim($_POST['title']);

	$_SESSION['step_1'] = array(
		'gurl' => $gurl,
		'gimg' => $gimg,
		'title' => $title
	);
}
#初始化第二步
$token = md5(uniqid().time());
$_SESSION['token'] = $token;

#载入model
load_model(array("user"));

#cache lite
include_once(_CONST_LOCAL_INCLUDE_PATH.'Cache/Lite.php');
include_once(_CONST_LOCAL_INCLUDE_PATH.'Cache/Lite/Output.php');

$cache = new Cache_Lite_Output();

#用于meta的keywords标签
$keywords = "";
#用于meta的Description标签
$description = "";
#用于mete的http-equiv标签
$referrer = "";
#用于meta的title标签
$title = "选择自己的照片 - Votings";
#用于获取针对本页的css样式
$css = array();
#用于获取针对本页的js
$js = array("jquery.masonry.min","ajaxupload");
#用于获取针对head的其他标签
$links = array();
#用于控制那个主菜单为
$o = "";

$ads = array();

$post_code =<<<HTML
HTML;

if (BENCHMARK) $timer->setMarker('proc');

//if ($user['loggedin'] < 1)
//	cache_header(900);

no_cache_header();

#header
include_once(_CONST_LOCAL_LAYOUT_PATH."_header.php");
?>
<script>
$(function(){
	//HIDE next-setp BUTTON
    $("#next-step").hide();
	//SELECT GIMG
	$(".pin").click(function(){
		$(".pin").removeClass("selected");
		$(this).addClass("selected");
		$("#myimg").val($(this).children().children("img").attr("src"));
		$("#next-step").show();
	});	
	
	//IMAGE STYLE
	var $container = $('#container');
	$container.imagesLoaded( function() {
		$container.masonry({
			itemSelector : '.pin',
			isAnimated: 'true'
		});
	});
	//UPLOAD
	var button = $('#upload-photo'), interval;
		
		new AjaxUpload(button, {
			action: 'ajax/upload-handler.php', 
			name: 'myfile',
			onSubmit : function(file, ext){
				// change button text, when user selects file			
				button.text('Uploading');
								
				// If you want to allow uploading only 1 file at time,
				// you can disable upload button
				this.disable();
				
				// Uploding -> Uploading. -> Uploading...
				interval = window.setInterval(function(){
					var text = button.text();
					if (text.length < 13){
						button.text(text + '.');					
					} else {
						button.text('Uploading');				
					}
				}, 200);
			},
			onComplete: function(file, response){
				button.text('Upload');
							
				window.clearInterval(interval);
							
				// enable upload button
				this.enable();
				if(response != 'error') {
					// add file to the list
					$(".pin").removeClass("selected")		
					$container.prepend( '<div class="pin selected"><a href="###"><img src="'+response+'"></a></div>' );
					$container.imagesLoaded( function(){
					  $container.masonry( 'reload' );
					});
					$("#myimg").val($("#container .selected").children().children("img").attr("src"));
					//show next-setp BUTTON
					$("#next-step").show();
					//SELECT GIMG
					$(".pin").click(function(){
						$(".pin").removeClass("selected");
						$(this).addClass("selected");
						$("#myimg").val($(this).children().children("img").attr("src"));
						$("#next-step").show();
					});	
				}	
			}
		});
		
	<?php
	if(isset($_SESSION['step_2'])){
	?>
	$("#myimg").val('<?php echo $_SESSION['step_2']['myimg']?>');
	$(".pin a img").each(function(i){
		if($(this).attr("src") == '<?php echo $_SESSION['step_2']['myimg']?>')
		{
			$(this).parent().parent().addClass('selected');
			return;
		}
	});
	//SHOW next-setp BUTTON
    $("#next-step").show();
	<?php
	}
	?>
});
</script>
<div id="main">
<form name="step-2" id="step-2" action="flow-3.php" method="post">
<input name="myimg" id="myimg" type="hidden" value="">
<input name="token" id="token" type="hidden" value="<?php echo $token;?>">
<input name="step" id="step" type="hidden" value="do">
	<div class="box">
		<ul class="flow clearfix f2">
			<li>1、选择商品图片</li>
			<li class="cur">2、选择自己的照片</li>
			<li>3、添加一段描述</li>
			<li>4、发布投票</li>
		</ul>
		<div class="box-header"><h1>选择自己的照片</h1></div>
		<div class="input-wrap clearfix">
			<input type="button" id="upload-photo" name="upload-photo" class="upload-photo" value="" />
			<span class="input-tips">You can upload your photo specially for this voting. It will not show anywhere else on the site.</span>
		</div>
		
		<div class="box-header"><h1>从相册选择</h1></div>
       <div id="container" class="photo-wrap">
	    <?php
		$rs = get_thumbfile_by_userid($_SESSION['userid']);
		if(sizeof($rs)>0){
			foreach($rs as $r) {
		?>
			<div class="pin">
				<a href="###"><img src="<?php echo _CONST_PIC_URL . 'thumb/' . $_SESSION['userid'] . "/1_" . $r['filename']?>" /></a>
			</div>
		<?php
			}
		}
		?>	
		</div>
		<div class="next-step"><input type="button" name="previous-step" class="previous" value="" onclick = "window.location.href='<?php echo _CONST_WEB_URL . 'flow-1.php'?>';" /><input type="button" name="next-step" id="next-step" class="next"  value="" onclick="vote.checkForm2();" /></div>
	</div>
</form>		
</div>
<!--main end-->
<?php
//footer
include_once (_CONST_LOCAL_LAYOUT_PATH."_footer.php");

if (BENCHMARK) $timer->setMarker('page');

if (BENCHMARK) {
	echo "<!-- init: ".$timer->timeElapsed('Start', 'init')." -->\n";
	echo "<!-- auth: ".$timer->timeElapsed('init', 'auth')." -->\n";
	echo "<!-- proc: ".$timer->timeElapsed('auth', 'proc')." -->\n";
	echo "<!-- page: ".$timer->timeElapsed('proc', 'page')." -->\n";
	echo "<!-- total: ".$timer->timeElapsed('Start', 'Stop')." -->\n";
}

?>
